#!/bin/bash
 
set -x

mkdir -p /media/deepin-livecd/squashfs-root
mount /dev/sdb2 /media/deepin-livecd/squashfs-root/
mkdir -p /media/deepin-livecd/squashfs-root/boot/efi
mount -t vfat /dev/sdb1 /media/deepin-livecd/squashfs-root/boot/efi/
cd /media/deepin-livecd

export LC_ALL=C
export DEBIAN_FRONTEND=noninteractive
export APT_LISTCHANGES_FRONTEND=none
export installer_url=https://mirrors.ustc.edu.cn/debian
export RELEASE=buster
export CURDIR=/media/deepin-livecd
export CHROOT=$CURDIR/squashfs-root
export PKG_CORE="busybox,systemd,systemd-sysv,initramfs-tools,live-boot"


function do_prep()
{
    apt install debootstrap squashfs-tools xorriso genisoimage isolinux syslinux-efi grub-pc-bin grub-efi-amd64-bin grub-efi-ia32-bin mtools dosfstools -y
}

#apt-get download `cat mini-base.list`

function do_first_stage()
{
rm -rvf  $CURDIR/squashfs-root/*
ln -sv /usr/share/debootstrap/scripts/sid /usr/share/debootstrap/scripts/apricot -f
debootstrap --no-check-gpg --variant=minbase  --include=$PKG_CORE --components=main,non-free,contrib --arch=amd64 $RELEASE $CHROOT $installer_url

cp -f /etc/hosts $CHROOT/etc/hosts
cp -f /etc/resolv.conf $CHROOT/etc/
mount --bind /dev/   $CHROOT/dev
mount -t proc proc   $CHROOT/proc
mount -t sysfs sysfs $CHROOT/sys

echo "deb $installer_url $RELEASE main non-free contrib"          >  ${CHROOT}/etc/apt/sources.list
echo 'APT::Install-Recommends "false";' > ${CHROOT}/etc/apt/apt.conf

cat <<'EOF' > ${CHROOT}/tmp/set-livecd.sh
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
#useradd -m -d /home/live -s /usr/bin/bash -G users live
#mkdir -p /home/live && chown -R live:live /home/live/
echo "root:password" | chpasswd 
##echo "deepin:112234" | chpasswd

apt update && apt upgrade -y
apt install --no-install-recommends isc-dhcp-client live-boot zssh openssh-server openssh-sftp-server openssh-client tar iptables nano vim isc-dhcp-client iproute2 deepin-keyring -y
EOF
chmod +x ${CHROOT}/tmp/set-livecd.sh
chroot $CHROOT "/tmp/set-livecd.sh" 
}


function do_second_stage()
{
echo "deb http://192.168.100.83:8082/deepin/apricot apricot main non-free contrib"          >  ${CHROOT}/etc/apt/sources.list
cp -rf "$CURDIR"/gpg/*gpg $CHROOT/etc/apt/trusted.gpg.d/
cat <<'EOF' > ${CHROOT}/tmp/set-livecd.sh
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
apt update && apt -f install -y
apt install --no-install-recommends live-config live-boot linux-image-deepin-hwe-amd64 sudo bash-completion xinit xserver-xorg console-setup net-tools grub-efi grub-pc-bin grub-efi-amd64-bin grub-efi-ia32-bin mtools -y
apt install --no-install-recommends tar wget curl vim nano git gvfs lzma unrar unzip zip p7zip -y
apt install --no-install-recommends rsync debootstrap squashfs-tools xorriso genisoimage mtools dosfstools -y
apt install --no-install-recommends parted gparted partclone mdadm lvm2 expect zssh openssh-server openssh-client python3 -y
update-initramfs -u
EOF
chmod +x ${CHROOT}/tmp/set-livecd.sh
chroot $CHROOT "/tmp/set-livecd.sh" 
echo "Port 22"  >> $CHROOT/etc/ssh/sshd_config
echo "PermitRootLogin yes"  >> $CHROOT/etc/ssh/sshd_config
echo "PasswordAuthentication yes"  >> $CHROOT/etc/ssh/sshd_config
echo "deepin-live" | tee "$CHROOT/etc/hostname"
rm -f  $CHROOT/etc/localtime
ln -sf /usr/share/zoneinfo/Asia/Shanghai $CHROOT/etc/localtime
echo '0.0 0 0' >  $CHROOT/etc/adjtime
echo '0'       >> $CHROOT/etc/adjtime
echo 'LOCAL'   >> $CHROOT/etc/adjtime
echo 'Asia/Shanghai'             >  $CHROOT/etc/timezone
echo 'export TZ=Asia/Shanghai'   >> $CHROOT/etc/profile
echo 'export LANGUAGE=zh_CN.UTF-8'   >> $CHROOT/etc/profile
echo 'export LANG=zh_CN.UTF-8'   >> $CHROOT/etc/profile
echo 'export LC_ALL=zh_CN.UTF-8' >> $CHROOT/etc/profile
echo '. /etc/bash_completion'   >> $CHROOT/etc/bash.bashrc
}

function do_third_stage()
{
cat <<'EOF' > ${CHROOT}/tmp/set-livecd.sh
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
apt install --no-install-recommends fonts-wqy-zenhei fonts-wqy-microhei fcitx fcitx-ui-classic fcitx-frontend-gtk2 fcitx-config-gtk fcitx-googlepinyin  -y
EOF
chmod +x ${CHROOT}/tmp/set-livecd.sh
chroot $CHROOT "/tmp/set-livecd.sh" 

# fcitx config
mkdir -p $CHROOT/etc/X11/Xsession.d
mkdir -p $CHROOT/etc/xdg/autostart
echo 'export GTK_IM_MODULE="xim"'    >  $CHROOT/etc/X11/Xsession.d/96fcitx_start
echo 'export XMODIFIERS="@im=fcitx"' >> $CHROOT/etc/X11/Xsession.d/96fcitx_start

cp -f "$CURDIR"/tools/settings/fcitx-config.desktop   $CHROOT/etc/xdg/autostart/
cp -f "$CURDIR"/tools/settings/*.desktop                   $CHROOT/usr/share/applications/
cp -f "$CURDIR"/tools/bin/*   			    $CHROOT/usr/local/bin
cp -f "$CHROOT"/usr/local/bin/97pinyin*                  $CHROOT/etc/X11/Xsession.d/
cp -f "$CHROOT"/usr/local/bin/fcitx-pinyin*  	   $CHROOT/usr/bin/

chmod +755 -R $CHROOT/usr/local/bin
chmod +644 -R $CHROOT/boot
chmod +755 -R $CHROOT/root
}

function do_four_stage()
{
cat <<'EOF' > ${CHROOT}/tmp/set-livecd.sh
#!/bin/bash
export DEBIAN_FRONTEND=noninteractive
apt install --no-install-recommends lightdm mate-desktop-environment-core epiphany-browser pluma engrampa eom mate-calc -y
EOF
chmod +x ${CHROOT}/tmp/set-livecd.sh
chroot $CHROOT "/tmp/set-livecd.sh" 
cd $CHROOT/usr/share/themes/ && rm -rf ContrastHigh* Clearlook* Crux* eOS* HighContrast* Industria* Mist* Raleigh* Redmond* Traditional*  &&  cd $CHROOT/

cd $CHROOT/
ln -sf pluma usr/bin/gedit
ln -sf caja  usr/bin/nautilus
ln -sf eom   usr/bin/eog
ln -sf engrampa      usr/bin/file-roller
ln -sf mate-terminal usr/bin/gnome-terminal

cd $CHROOT/
cd usr/share/applications
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' engrampa.desktop
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' mate-terminal.desktop
sed -i 's/GenericName\[zh_CN\]/Name\[zh_CN\]/' caja-browser.desktop
sed -i 's/Exec=fcitx/Exec=im-config/' fcitx.desktop
sed -i 's/Settings/System/' fcitx-con*.desktop
sed -i 's/NoDisplay\=true//' python*.desktop
sed -i 's/System\;//'  mate-terminal.desktop
sed -i 's/System\;//'  caja-browser.desktop
sed -i 's/MATE\ 之眼//' eom.desktop
cd ../../../

}

############################################
function do_cleanup()
{
### clean up and umountx
cd $CURDIR/squashfs-root
### Remove upgraded, old linux-kernels if more than one
#ls /boot/vmlinuz-*.*.**-**-generic > list.txt
#sum=$(cat list.txt | grep '[^ ]' | wc -l)
#if [ $sum -gt 1 ]; then
#dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs apt -y purge
#fi
#rm list.txt

chroot . rm -f etc/init/tty3.conf etc/init/tty4.conf etc/init/tty5.conf
#chroot . rm -f etc/resolv.conf
chroot  $CHROOT/ apt-get clean
chroot  $CHROOT/ apt-clean
### umountx
chroot $CHROOT apt clean
rm -rf $CHROOT/tmp/*
rm -f $CHROOT/vmlinu* $CHROOT/ initrd.im*
rm -rvf $CHROOT/var/cache/apt/archives/ 
cd ..

}

function do_umount()
{
cd $CHROOT/
chroot . rm -f /var/lib/dbus/machine-id
chroot . rm -f /sbin/initctl
chroot . dpkg-divert --rename --remove /sbin/initctl
umount -lf $CHROOT/dev/pts/
umount -lf $CHROOT/dev
umount -lf $CHROOT/proc
umount -lf $CHROOT/sys
}

function do_mk_file()
{

cat <<'EOF' > $HOME/LIVE_BOOT/scratch/grub.cfg

search --set=root --file /DEBIAN_CUSTOM

insmod all_video

set default="0"
set timeout=30

menuentry "DDE Desktop Live" {
    linux /vmlinuz boot=live union=overlay quiet
    initrd /initrd
}
EOF

touch $HOME/LIVE_BOOT/image/DEBIAN_CUSTOM
grub-mkstandalone --format=x86_64-efi --output=$HOME/LIVE_BOOT/scratch/bootx64.efi --locales="" --fonts="" "boot/grub/grub.cfg=$HOME/LIVE_BOOT/scratch/grub.cfg"
cd $HOME/LIVE_BOOT/scratch                      && \
dd if=/dev/zero of=efiboot.img bs=1M count=10	&& \
mkfs.vfat efiboot.img							&& \
mmd -i efiboot.img efi efi/boot					&& \
mcopy -i efiboot.img ./bootx64.efi ::efi/boot/

}

function do_mkiso()
{

xorriso -as mkisofs \
    -iso-level 3 \
    -full-iso9660-filenames \
    -volid "DEBIAN_CUSTOM" \
    -eltorito-alt-boot \
    -e EFI/efiboot.img \
    -no-emul-boot \
    -append_partition 2 0xef ${HOME}/LIVE_BOOT/scratch/efiboot.img \
    -graft-points "${HOME}/LIVE_BOOT/image" /EFI/efiboot.img=$HOME/LIVE_BOOT/scratch/efiboot.img \
    -output "${HOME}/LIVE_BOOT/Deepin-15.9.2-custom-LiveCD.iso"
}

do_prep
do_first_stage
do_second_stage
do_third_stage
do_four_stage
do_umount
do_clean
#do_mk_file
#do_mkiso
